/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.panels;

import nl.biz_e.gwt.baselib.ui.client.IAppManager;
import nl.biz_e.gwt.baselib.ui.client.menu.MenuPanelManager;

import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @author Joost A. Bloemsma
 */
public class PanelManager {
    /**
     * Default id for the main div
     */
    public static final String MAINDIV = "mainDiv";
    /**
     * Default id for the menu div
     */
    public static final String MENUDIV = "biz_eMenuBar";
    /**
     * Default id for the Application div
     */
    public static final String APPDIV = "biz_eAppPanel";
    /**
     * Default id for the div with the login/-out link
     */
    public static final String LOGINDIV = "logInOut";
    private final String mainPanelId;
    private final String menuPanelId;
    private final String appPanelId;
    private final String loginPanelId;
    private final MenuPanelManager menuManager;
    private IAppPanel currentPanel;
    private final IAppManager appManager;

    /**
     * Constructs a new PanelManager instance
     */
    public PanelManager() {
        this(null);
    }

    /**
     * Constructs a new PanelManager instance
     * 
     * @param appManager
     *            implementation of {@link IAppManager}.
     */
    public PanelManager(IAppManager appManager) {
        this(MAINDIV, MENUDIV, APPDIV, LOGINDIV, appManager);
    }

    /**
     * Constructs a new PanelManager instance, setting the div names
     * 
     * @param mainPanelId
     * @param menuPanelId
     * @param appPanelId
     * @param loginPanelId
     */
    public PanelManager(String mainPanelId, String menuPanelId,
            String appPanelId, String loginPanelId) {
        this(mainPanelId, menuPanelId, appPanelId, loginPanelId, null);
    }

    /**
     * Constructs a new PanelManager instance, setting the div names
     * 
     * @param mainPanelId
     * @param menuPanelId
     * @param appPanelId
     * @param loginPanelId
     * @param appManager
     *            implementation of {@link IAppManager}.
     */
    public PanelManager(String mainPanelId, String menuPanelId,
            String appPanelId, String loginPanelId, IAppManager appManager) {
        this.appManager = appManager;
        this.mainPanelId = mainPanelId;
        this.menuPanelId = menuPanelId;
        this.appPanelId = appPanelId;
        this.loginPanelId = loginPanelId;
        menuManager = new MenuPanelManager(this.menuPanelId, this.loginPanelId,
                this);
        if (this.appManager != null) {
            menuManager.addMenuItems(this.appManager.getMenuItems());
        }
    }

    /**
     * Shows a new panel after closing the current one.<br/>
     * For panel lifecycle see {@link IAppPanel}.
     * 
     * @param panel
     */
    public void showPanel(IAppPanel panel) {
        if (!(panel instanceof Widget))
            throw new IllegalArgumentException(
                    "An IAppPanel implementation must extend the com.google.gwt.user.client.ui.Widget class.");
        if (currentPanel != null) {
            if (!currentPanel.prepareForClose()) {
                return;
            } else {
                menuManager.removeMenuItems(currentPanel.getMenuItems());
            }
        }
        this.getAppPanel().clear();
        panel.setPanelManager(this);
        this.getAppPanel().add((Widget) panel);
        if (currentPanel == null) {
            // FIrst panel, so include application menuitems as well
            menuManager.addMenuItems(appManager.getMenuItems());
        }
        menuManager.addMenuItems(panel.getMenuItems());
        this.currentPanel = panel;

    }

    /**
     * @return the panel holding the application panel
     */
    public Panel getAppPanel() {
        return RootPanel.get(this.appPanelId);
    }

    /**
     * @return the main panel
     */
    public Panel getMainPanel() {
        return RootPanel.get(this.mainPanelId);
    }

    /**
     * @return the mainPanelId
     */
    public String getMainPanelId() {
        return mainPanelId;
    }

    /**
     * @return the menuPanelId
     */
    public String getMenuPanelId() {
        return menuPanelId;
    }

    /**
     * @return the appPanelId
     */
    public String getAppPanelId() {
        return appPanelId;
    }

    /**
     * @return the loginPanelId
     */
    public String getLoginPanelId() {
        return loginPanelId;
    }

    /**
     * @return the menuManager
     */
    public MenuPanelManager getMenuManager() {
        return menuManager;
    }

    /**
     * @return the appManager
     */
    public IAppManager getAppManager() {
        return appManager;
    }
}
